﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Data;
using System.Data.Common;
using System.Data.SQLite;

namespace YourMoney
{
    public class Tables
    {
        /// <summary>
        /// Создание таблиц БД и вставка начальных данных
        /// </summary>
        public Tables()
        {
            if (!File.Exists("Money.db3"))
            {
                SQLiteDriver drv = SQLiteDriver.GetDrv("Money.db3");
                string command = @"CREATE TABLE IF NOT EXISTS [TypeOfEssence] (
                     [id] integer PRIMARY KEY AUTOINCREMENT NOT NULL,
                     [parent_id] integer NOT NULL,
                     [root_id] integer NOT NULL,
                     [is_root] integer NOT NULL,
                     [name] char(100) NOT NULL,
                     [color] int NOT NULL,
                     FOREIGN KEY(parent_id) REFERENCES TypeOfEssence(id)
                     );";
                drv.NonExecuteQuery(command);
                command = @"CREATE TABLE IF NOT EXISTS [Essence] (                     
                     [id] integer PRIMARY KEY AUTOINCREMENT NOT NULL,
                     [type_id] integer NOT NULL,
                     [size] int NOT NULL,
                     [date] integer NOT NULL,
                     [comment] char(100) NOT NULL,
                     FOREIGN KEY(type_id) REFERENCES TypeOfEssence(id)
                     );";
                drv.NonExecuteQuery(command);
                command = @"INSERT INTO 'TypeOfEssence'
                          ('parent_id','root_id','is_root','name','color') VALUES (0,1,1,'Доходы',-16711936);";
                drv.NonExecuteQuery(command);
                command = @"INSERT INTO 'TypeOfEssence'
                          ('parent_id','root_id','is_root','name','color') VALUES (0,2,1,'Расходы',-65536);";
                drv.NonExecuteQuery(command);
                command = @"INSERT INTO 'TypeOfEssence'
                          ('parent_id','root_id','is_root','name','color') VALUES (1,1,0,'Зарплата',-8323200);";
                drv.NonExecuteQuery(command);

                command = @"INSERT INTO 'TypeOfEssence' 
                          ('parent_id','root_id','is_root','name','color') VALUES (2,1,0,'Подарки',-4972010);";
                drv.NonExecuteQuery(command);

                command = @"INSERT INTO 'TypeOfEssence'
                          ('parent_id','root_id','is_root','name','color') VALUES (2,2,0,'Интернет',-5042413);";
                drv.NonExecuteQuery(command);

                command = @"INSERT INTO 'TypeOfEssence'
                          ('parent_id','root_id','is_root','name','color') VALUES (2,2,0,'Еда',-5157337);";
                drv.NonExecuteQuery(command);

                command = @"INSERT INTO 'TypeOfEssence'
                          ('parent_id','root_id','is_root','name','color') VALUES (2,2,0,'Девочки',-3926680);";
                drv.NonExecuteQuery(command);

                command = @"INSERT INTO 'TypeOfEssence'
                          ('parent_id','root_id','is_root','name','color') VALUES (1,1,0,'Коррупция',-16744448);";
                drv.NonExecuteQuery(command);
                command = @"INSERT INTO 'TypeOfEssence'
                          ('parent_id','root_id','is_root','name','color') VALUES (8,1,0,'В вузе',-16744984);";
                drv.NonExecuteQuery(command);
                command = @"INSERT INTO 'TypeOfEssence'
                          ('parent_id','root_id','is_root','name','color') VALUES (8,1,0,'На местах',-16711808);";
                drv.NonExecuteQuery(command);
                drv.Close();
            }
        }
        }
    }